package com.cskaoyan.user.controller;


import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.result.ResponseData;

import com.cskaoyan.mall.commons.util.CookieUtil;
import com.cskaoyan.user.dto.*;
import com.cskaoyan.user.service.IKaptchaService;
import com.cskaoyan.user.service.IUserLogin;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ArrayUtils;


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * /user/login
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserLoginController {
    @Autowired
    IKaptchaService iKaptchaService;
    @Autowired
    IUserLogin iUserLogin;

    @PostMapping("/login")
    public ResponseData login(@RequestBody Map<String, String> map, HttpServletRequest request, HttpServletResponse response) {
        String captcha = map.get("captcha");

        //验证码验证
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();

        String kaptch_uuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
        kaptchaCodeRequest.setUuid(kaptch_uuid);
        kaptchaCodeRequest.setCode(captcha);
        KaptchaCodeResponse kaptchaCodeResponse = iKaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        String imageCode = kaptchaCodeResponse.getCode();
        if (!imageCode.equals(SysRetCodeConstants.SUCCESS.getCode())) {

            return ResponseData.msg(kaptchaCodeResponse.getMsg());
        }

        UserLoginRequest userLoginRequest = new UserLoginRequest();
        userLoginRequest.setUserName(map.get("userName"));
        userLoginRequest.setPassword(map.get("userPwd"));
        UserLoginResponse userLoginResponse;
        userLoginResponse = iUserLogin.login(userLoginRequest);
        if (userLoginResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            Cookie cookie = CookieUtil.genCookie("access_token", userLoginResponse.getToken(), "/", 6000);
            cookie.setHttpOnly(true);
            cookie.setMaxAge(60 * 60 * 30);
            response.addCookie(cookie);

            return ResponseData.successAndResult(userLoginResponse);
        }


        return ResponseData.msg(userLoginResponse.getMsg());

    }

    @GetMapping("/login")
    public ResponseData login(HttpServletRequest request) {
        UserLoginVerifyResponse userLoginVerifyResponse;

        String user_info = request.getHeader("user_info");

        userLoginVerifyResponse = iUserLogin.loginVerify(user_info);

        String code = userLoginVerifyResponse.getCode();
        if (code.equals(SysRetCodeConstants.SUCCESS.getCode())) {
            Map<String, String> result = new HashMap<>();
            result.put("uid", userLoginVerifyResponse.getUid());
            result.put("file", userLoginVerifyResponse.getFile());
            result.put("username", userLoginVerifyResponse.getUsername());
            return ResponseData.successAndResult(result);
        }
        return ResponseData.msg(userLoginVerifyResponse.getMsg());
    }

    @GetMapping("/loginOut")
    public ResponseData loginOut(HttpServletRequest request, HttpServletResponse response) {
        Cookie[] cookies = request.getCookies();
        if (!ArrayUtils.isEmpty(cookies)) {
            for (Cookie cookie : cookies) {
                String name = cookie.getName();
                if ("access_token".equals(name)) {
                    cookie.setMaxAge(0);

                    cookie.setPath("/");

                    response.addCookie(cookie);


                }

            }
        }

        return ResponseData.successNoResult();
    }
}
